package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.GateSwtich;
import com.huitone.gddw.dao.worktable.IGateSwtichDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IGateSwtichService;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.dto.GateSwtichDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信网关双线切换日志 接口实现类
 * @author wuluodan
 *
 */
@Service
public class GateSwtichService implements IGateSwtichService {

	@Autowired
	private IGateSwtichDao gateSwtichDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<GateSwtichDTO> selectPage(QueryPage<GateSwtich> page) {
		List<GateSwtichDTO> result = gateSwtichDao.selectPage();
		
		QueryPage<GateSwtichDTO> queryPage = new QueryPage<GateSwtichDTO>();
		queryPage.setResult(result);
		return queryPage;
	}

	@Override
	public GateSwtich selectByPrimaryKey(String recId) {
		return gateSwtichDao.selectByPrimaryKey(recId);
	}

	@Override
	public void changeLineType(String recId, String lineType) {
		if (StringUtils.isNotBlank(recId)) {
			GateSwtich entity = this.selectByPrimaryKey(recId);
			entity.setLineType(lineType);
			this.update(entity);
		} else {
			gateSwtichDao.updateAll(lineType);
			this.updateParamRec("短信网关双线切换日志--一键切换");
		}
	}
	
	@Override
	public void save(GateSwtich entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		gateSwtichDao.insertSelective(entity);
		
		this.updateParamRec("短信网关双线切换日志--保存");
	}
	
	/**
	 * 更新
	 * @param entity GateSwtich
	 */
	private void update(GateSwtich entity) {
		entity.setSwitchTime(new Date());
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		gateSwtichDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("短信网关双线切换日志--更新");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.GATE_SWTICH, desc);
	}

	@Override
	public void deleteByGateLinkId(String gateLinkId) {
		gateSwtichDao.deleteByGateLinkId(gateLinkId);
	}

	@Override
	public void autoChangeSwitch(String status) {
		gateSwtichDao.autoSwitch(status);
	}

}
